<?php

namespace App\Admin\Controllers;

use Slowlyo\OwlAdmin\Controllers\AdminController;
use Slowlyo\OwlAdmin\Renderers\Page;
use Slowlyo\OwlAdmin\Renderers\Card;
use Slowlyo\OwlAdmin\Renderers\Form;
use Slowlyo\OwlAdmin\Renderers\Alert;
use Slowlyo\OwlAdmin\Renderers\ButtonGroup;
use Slowlyo\OwlAdmin\Renderers\Button;
use App\Models\Idcnode;

class FlowCheckController extends AdminController
{
    protected $categories = [
        1 => '客户侧流量统计',
        2 => '供应商流量统计',
        3 => '运营商流量统计'
    ];

    public function index(): Page
    {
        $alert = Alert::make()
            ->showIcon(true)
            ->body('对导出的流量数据进行除以1.024的修正，上传csv文件来对文件内的流量数据进行统一的除1.024操作');

        $form = Form::make()
            ->mode('normal')
            ->body([
                \Slowlyo\OwlAdmin\Renderers\FileControl::make()
                    ->name('file')
                    ->label('文件')
                    ->drag(true)
                    ->accept('.csv')
                    ->required(true)
            ]);

        // 从数据库获取唯一的idc_belong值
        $idcBelongs = Idcnode::select('idc_belong')->distinct()->pluck('idc_belong')->toArray();
        
        $buttons = [];
        foreach ($idcBelongs as $belong) {
            if (isset($this->categories[$belong])) {
                $buttons[] = Button::make()
                    ->label($this->categories[$belong] . ' 乘1.024')
                    ->level('primary')
                    ->data(['type' => 'multiply', 'belong' => $belong]);
                    
                $buttons[] = Button::make()
                    ->label($this->categories[$belong] . ' 除1.024')
                    ->level('default')
                    ->data(['type' => 'divide', 'belong' => $belong]);
            }
        }

        $buttonGroup = ButtonGroup::make()->buttons($buttons);

        return Page::make()
            ->title('流量数据修正')
            ->body([
                $alert,
                Card::make()->body([
                    $form,
                    $buttonGroup
                ])
            ]);
    }
} 